package com.fenghongzhang.day010;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private ContentResolver contentResolver;
    private Uri parse;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //解析者
         contentResolver = getContentResolver();

        //路径
        //https://blog.csdn.net/shuai_ge_feng/article/details/95967850
         parse = Uri.parse("content://com.feng.day009");

    }

    public void insert(View view) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","大牛");
        contentValues.put("price",30);
        contentResolver.insert(parse,contentValues);
    }

    public void del(View view) {
        contentResolver.delete(parse,"id = ?",new String[]{"2"});
    }

    public void update(View view) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","雪里红");
        contentResolver.update(parse,contentValues,"id = ?",new String[]{"1"});
    }

    public void select(View view) {

        //查询
        Cursor query = contentResolver.query(parse, null, null, null, null);
        if(query != null){
            while (query.moveToNext()){
                String name = query.getString(query.getColumnIndex("name"));
                Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
            }
            query.close();
        }
    }
}
